import React from "react";
import { Link, useParams, Outlet, useRoutes, Navigate } from "react-router-dom";

const Hot = () => <div>这是热门页</div>;
const New = () => <div>这是最新页</div>;

const Home = () => {
  return (
    <>
      <div>这是首页</div>
      <ul>
        <li>
          <Link to="hot">热门页</Link>
        </li>
        <li>
          <Link to="new">最新页</Link>
        </li>
      </ul>
      <Outlet />
    </>
  );
};
const About = () => <div>这是关于页</div>;
const Detail = () => {
  const params = useParams();
  return <div>这是详情页 - {params.id}</div>;
};

const routes = [
  {
    path: "/",
    element: <Navigate to="/home"></Navigate>,
  },
  {
    path: "/home",
    element: <Home />,
    children: [
      {
        path: "hot",
        element: <Hot />,
      },
      {
        path: "new",
        element: <New />,
      },
    ],
  },
  {
    path: "/about",
    element: <About />,
  },
  {
    path: "/detail/:id",
    element: <Detail />,
  },
  {
    path: "*",
    element: <div>404</div>,
  },
];

const App = () => {
  const element = useRoutes(routes);

  return (
    <div>
      <h2>router-v6-路由表</h2>
      <ul>
        <li>
          <Link to="/home">首页</Link>
        </li>
        <li>
          <Link to="/about">关于页</Link>
        </li>
        <li>
          <Link to="/detail/234">详情页</Link>
        </li>
      </ul>

      <hr />

      {element}

      {/* <Routes>
        <Route path="/" element={<Navigate to="/home"></Navigate>}></Route>
        <Route path="/home" element={<Home />}>
          <Route path="hot" element={<Hot />}></Route>
          <Route path="new" element={<New />}></Route>
        </Route>
        <Route path="/about" element={<About />}></Route>
        <Route path="/detail/:id" element={<Detail />}></Route>
        <Route path="*" element={<div>404</div>}></Route>
      </Routes> */}
    </div>
  );
};

export default App;
